﻿@using LanDBom.Models

@using LanDBom.Resources
@{
    ViewBag.Title = "Employee_management";
}
@section styles{
    <style>
        #E_Hidden_panel {
            border: 1px solid black;
            background-color: #E8ECF6;
            width: 660px;
            position: fixed;
            left: 40vh;
            top: 20vh;
        }

        input {
            width: 80%;
        }

        thead td {
            text-align: center;
        }

        input[type="password"]::-ms-reveal {
            display: none;
        }

        input[type="password"]::-ms-clear {
            display: none;
        }
    </style>
}


<from>
    <div>
        <fieldset class="boun nav" style="padding-bottom:6px;">
            <legend class="len">@Resource.String1235</legend>
            <div class="col-lg-3">@Resource.String1236:<input type="text" style="width:150px;" id="txtid" /></div>
            <div class="col-lg-3">@Resource.String1237:<input type="text" style="width: 150px;" id="txtname" /></div>
            <div class="col-lg-3">@Resource.String1238:<input type="text" style="width: 150px;" id="txtstanding" /></div>
            <div calss="col-lg-1"><button id="find_E" class="btn-sm  btn-secondary">@Resource.String1239</button></div>
        </fieldset>
    </div>
    <div><button class="btn" id="E_xzyg">@Resource.String1240</button></div>
    <table class="table gun tab">
        <thead>
            <tr>
                <td>@Resource.String1241</td>
                <td>@Resource.String1242</td>
                <td>@Resource.String1243</td>
                <td>@Resource.String1244</td>
                <td>@Resource.String1245</td>
                <td>@Resource.String1246</td>
                <td>邮箱</td>
                <td colspan="2">@Resource.String1247</td>
            </tr>
        </thead>
        <tbody id="E_tb_e1" style="height:500px">
        </tbody>
    </table>

    <div id="E_Hidden_panel" style="display:none">
        <div class="Tuodong"><span>     长按拖动      </span></div>
        <div style="text-align:right"><span id="E_Add_tc" style="font-size:18px;margin-right:5px"><i class="bi bi-x-square-fill" style="font-size:18px;color:coral"></i></span></div>
        <fieldset class="boun-x">
            <legend class="len-x">@Resource.String1248</legend>
            <table class="table">
                <thead style="display:none;"><tr><td id="RYID"></td></tr></thead>
                <tbody>
                    <tr>
                        <td>@Resource.String1249</td>
                        <td><input type="text" id="E_name" class="input-group-text" /></td>
                    </tr>
                    <tr>
                        <td>@Resource.String1250</td>
                        <td><input type="password" value="111111" id="E_mi" class="input-group-text" data="passworedtype" /></td>
                    </tr>
                    <tr>
                        <td>@Resource.String1251</td>
                        <td><input type="text" id="E_lian" class="input-group-text" /></td>
                    </tr>
                    <tr>
                        <td>@Resource.String1252</td>
                        <td>
                            <select id="E_shengf" class="input-group-text">
                                <option value="null">@Resource.String1255</option>
                                <option value="管理员">管理员</option>
                                <option value="员工">员工</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>@Resource.String1253</td>
                        <td>
                            <select id="E_bumeng" class="input-group-text">
                                <option value="null">@Resource.String1255</option>
                                @foreach (var item in ViewBag.List_bu as List<F_BuMen>)
                                {
                                    <option value="@item.FID" id="CL_ID">@item.FName</option>
                                }
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>@Resource.String1254</td>
                        <td>
                            <select id="E_zhiwei" class="input-group-text">
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>@Resource.String218</td>
                        <td>
                            <input id="E_EMial" class="input-group-text" type="email" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <div>
                <button style="text-align:left" id="E_Add_OK" class="btn-sm btn-secondary">@Resource.String1230</button>
            </div>
        </fieldset>
    </div>
</from>



@section scripts
{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(function () {
            GetEmployee()
            //点击查找
            $(document).on("click", "#find_E", function () {
                GetEmployee()
            })
            //点击修改员工信息
            $(document).on("click", "#E_xgxx", function (e) {
                if ($(e.target).text() == "修改信息") {
                    var name = $(e.target).parent().children().eq(8).text()
                    $.ajax({
                        url: '/Admin/Employee_management',
                        type: 'GET',
                        data: { "HUNF": name,  "state": 3 },
                        success: function (data) {
                            console.log(data)
                            $.each(data, function (i, item) {
                                $("#E_bumeng").val(item.DE_bumeng);
                                $("#E_bumeng").trigger("change")
                                Open_panel()
                                $("#E_name").val(item.DE_nae);
                                $("#RYID").text(item.DE_ID);
                                $("#E_mi").val(item.DE_Pass);
                                $("#E_shengf").val(item.DE_quan);
                                $("#E_lian").val(item.DE_phone);
                                $("#E_EMial").val(item.DE_email);
                                setTimeout(function () {
                                    $("#E_zhiwei").val(item.DE_shenfen)
                                },300)
                            })
                        },
                        error: function () {
                            alert('Request Error');
                        }
                    })


                }
            })

            //打开新增员工页面
            $(document).on("click", "#E_xzyg", function () {
                Open_panel()

            })
            $(document).on("click", "#E_Add_tc", function () {
                Close_page()
            })
            //新增员工
            $(document).on("click", "#E_Add_OK", function () {
                var A = $("#E_name").val();
                var AB = $("#E_shengf").val();
                var CA = $("#E_lian").val();
                var AD = $("#E_bumeng").val();
                var AE = $("#E_zhiwei").val();
                if (A == "" || AB == null || CA == "") {
                    alert("@Resource.String1256")
                } else if (AD == null || AE == null) {
                    alert("@Resource.String1257")
                } else {
                    New_employee()
                    GetEmployee()
                }
            })

            //删除员工
            $(document).on("click", "#E_scyg", function (x) {
                if (confirm("是否删除该行")) {
                    Deletion_of_employee($(x.target))
                }
            })
            //查找员工的方法
            function GetEmployee() {
                var name = $("#txtname").val()
                var id = $("#txtid").val()
                var standing = $("#txtstanding").val()
                $.ajax({
                    url: '/Admin/Employee_management',
                    type: 'GET',
                    data: { "id": id, "name": name, "standing": standing, "state": 1 },
                    success: function (data) {
                        createRow(data)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            //打印员工的方法
            function createRow(data) {
                $('#E_tb_e1 tr').remove();
                $.each(data, function (i, item) {
                    var $tr = $(`
                                                <tr>
                                                    <td id="E_name_e1">${item.DE_nae}</td>
                                                    <td id="E_mi_e1">${item.DE_Pass}</td>
                                                    <td id="E_shenf_e1">${item.DE_quan}</td>
                                                    <td id="E_lian_e1">${item.DE_phone}</td>
                                                    <td id="E_bu_e1">${item.FName_B}</td>
                                                    <td id="E_zhi_e1">${item.FName_S}</td>
                                                    <td id="E_zhi_e1">${item.DE_email}</td>
                                                    <td id="E_xgxx">@Resource.String1258</td>
                                                    <td style="display:none" id="E_id_e1">${item.DE_ID}</td>
                                                    <td id="E_scyg">@Resource.String1259</td>
                                                </tr>
                                            `);
                    $('#E_tb_e1').append($tr);
                })
            }
            //打开新增页面的方法
            function Open_panel() {
                $("#E_Hidden_panel").css({ "display": "block" });
            }
            //关闭新增页的方法
            function Close_page() {
                $("#E_Hidden_panel").css({ "display": "none" })
            }
            //新增员工的方法
            function New_employee() {
                var HFNH = $("#E_zhiwei").val()
                HFNH = HFNH == 3 || HFNH == 5 || HFNH == 10 ? "1" : HFNH == 6 || HFNH == 4 || HFNH == 11 || HFNH == 9 ? "2" : HFNH == 7 || HFNH == 8 || HFNH == 10 ? "3":"0";
                var employee = {
                    DE_ID: $("#RYID").text(),
                    DE_nae: $("#E_name").val(),
                    DE_Pass: $("#E_mi").val(),
                    DE_quan: $("#E_shengf").val(),
                    DE_phone: $("#E_lian").val(),
                    DE_bumeng:$("#E_bumeng").val(),
                    DE_shenfen: $("#E_zhiwei").val(),
                    DE_zhiji: HFNH,
                    DE_email: $("#E_EMial").val(),
                    DE_zhuang: "在职"
                }
                console.log(employee)
                $.ajax({
                    url: "/Admin/Add_employee",
                    type: "POST",
                    data: employee,
                    success: function (data) {
                        console.log(data)
                        alert(data)
                        Close_page()
                    },
                    error: function (data) {
                        alert("erroer");
                    },
                })
            }
            //修改员工信息的方法
            function Modify_employee_information(target) {
                var name = target.parent().children().eq(0).children().val()
                var pass = target.parent().children().eq(1).children().val()
                var sf = target.parent().children().eq(2).children().val()
                var lian = target.parent().children().eq(3).children().val()
                var bu = target.parent().children().eq(4).children().val()
                var zhi = target.parent().children().eq(5).children().val()

                var dengl = {
                    DE_ID: target.next().text(),
                    DE_nae: name,
                    DE_Pass: pass,
                    DE_quan: sf,
                    DE_phone: lian,
                    DE_bumeng:bu,
                    DE_shenfen:zhi
                }
                console.log(dengl)
                if (lian.trim().length > 11) {
                    $("#E_in_lian").css({ "color": "red" });
                    return
                }
                $.ajax({
                    url: "/Admin/Modify_employee_information",
                    type: "POST",
                    data: { "dl": dengl },
                    success: function (path) {
                        location.reload(true);
                        alert(path)
                    },
                    error: function (path) {
                        alert("系统错误!");
                    }
                })
            }
            //删除员工的方法
            function Deletion_of_employee(target) {
                var dengl = {
                    DE_ID: target.prev().text(),
                    DE_zhuang: "离职"
                }
                console.log(dengl)
                $.ajax({
                    url: "/Admin/Deletion_of_employee",
                    type: "POST",
                    data: { "dl": dengl },
                    success: function (path) {
                        alert(path)
                    },
                    error: function (path) {
                        alert("系统错误!");
                    }
                })
            }
            //根据部门修改职位的方法
            function find_zhi(e) {
                var index = e.val()
                $.ajax({
                    url: "/Admin/Employee_management",
                    type: "POST",
                    data: { "id": index, "state": 2 },
                    success: function (path) {
                        $('#E_zhiwei option').remove();
                        $('#E_zhiwei').append($(`<option value="0" id="CL_ID">—@Resource.String1255—</option>`));
                        $.each(path, function (i, item) {
                            var $tr = $(`<option value="${item.FID}" id="CL_ID">${item.FName}</option>`);
                        $('#E_zhiwei').append($tr);
                        })
                    },
                    error: function () {
                        alert("@Resource.String1260!");
                    }
                })
            }
            //根据部门修改职位的方法
            function find_zhi2(e) {
                var index = e.val()
                $.ajax({
                    url: "/Admin/Employee_management",
                    type: "POST",
                    data: { "id": index ,"state":2},
                    success: function (path) {
                        $('#E_i_sf option').remove();
                        $('#E_i_sf').append($(`<option value="0" id="CL_ID">—@Resource.String1255—</option>`));
                        $.each(path, function (i, item) {
                            var $tr = $(`<option value="${item.FID}" id="CL_ID">${item.FName}</option>`);
                            $('#E_i_sf').append($tr);
                        })
                    },
                    error: function (path) {
                        alert("@Resource.String1260!");
                    }
                })
            }
            //部门下拉框修改触发事件
            $(document).on("change", "#E_bumeng", function (e) {
                find_zhi($(e.target))
            })
            $(document).on("change", "#E_in_sf", function (e) {

                    find_zhi2($(e.target))
            })

        })
    </script>
}